package com.aptemo.webetek.client.widgets.players;

import java.util.List;
import java.util.MissingResourceException;

import com.aptemo.webetek.client.Webetek;
import com.aptemo.webetek.client.enums.EnumUserStatus;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.aptemo.webetek.client.utils.TokenUtils;
import com.aptemo.webetek.client.widgets.dialogs.InvitationDialog;
import com.aptemo.webetek.client.widgets.paging.PagingDisplay;
import com.aptemo.webetek.client.widgets.paging.PagingView;
import com.aptemo.webetek.shared.UserBean;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.HasChangeHandlers;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class Players
		extends Composite implements PlayersDisplay
{

	private static PlayersUiBinder	uiBinder	= GWT.create(PlayersUiBinder.class);

	interface PlayersUiBinder extends UiBinder<Widget, Players>
	{
	}

	@UiField
	FlexTable					contentPanel;

	@UiField
	Button						createNewPlayerButton;

	@UiField
	ListBox						statusFilter;

	@UiField
	PagingView					pagingPanel;

	@UiField
	Label						infoField;

	// TODO : not yet...
	// @UiField
	Label						subjectField		= new Label(Webetek.getConstants().invitationSubject());

	private InvitationDialog	invitationDialog	= new InvitationDialog("Invitation message...");

	public Players()
	{
		initWidget(uiBinder.createAndBindUi(this));

		EnumUserStatus[] statuses = EnumUserStatus.values();
		for (EnumUserStatus item : statuses)
		{
			statusFilter.addItem(translate(item.name()), item.toString());
		}

		statusFilter.setItemSelected(0, true);
	}

	@Override
	public Widget getDisplay()
	{
		return this;
	}

	@Override
	public int getClickedRow(ClickEvent event)
	{
		int selectedRow = -1;
		HTMLTable.Cell cell = contentPanel.getCellForEvent(event);

		if (cell != null)
		{
			// Suppress clicks if the user is actually selecting the
			// check box
			//
			if (cell.getCellIndex() >= 0)
			{
				selectedRow = cell.getRowIndex();
			}
		}

		return selectedRow;
	}

	@Override
	public HasClickHandlers getList()
	{
		return contentPanel;
	}

	@Override
	public void setData(List<UserBean> data)
	{
		contentPanel.removeAllRows();

		if (data.size() == 0)
		{
			contentPanel.setText(1, 0, "No entries...");
		}
		int selind = statusFilter.getSelectedIndex();
		String status = statusFilter.getValue(selind);

		int i = 0;
		for (UserBean item : data)
		{
			PlayerRow row = new PlayerRow(item);
			contentPanel.setWidget(i++, 0, row);
		}
	}

	@Override
	public HasChangeHandlers getStatusListHandler()
	{
		return statusFilter;
	}

	@UiHandler("createNewPlayerButton")
	void invitePlayerHandler(ClickEvent event)
	{
		invitationDialog.center();
	}

	@Override
	public PagingDisplay getPagingView()
	{
		return pagingPanel;
	}

	@Override
	public HasDecisionHandlers getInvitationConfirmation()
	{
		return invitationDialog;
	}

	@Override
	public HasText getInvitationEmailField()
	{
		return invitationDialog.getEmailField();
	}

	@Override
	public HasText getInvitationMessageField()
	{
		return invitationDialog.getMessageField();
	}

	@Override
	public EnumUserStatus getStatusFilterValue()
	{
		int i = statusFilter.getSelectedIndex();

		String s = statusFilter.getValue(i);
		if (s.isEmpty())
		{
			return null;
		}

		return EnumUserStatus.valueOf(s);
	}

	@Override
	public HasText getInfoField()
	{
		return infoField;
	}

	@Override
	public HasText getInvitationSubjectField()
	{
		return subjectField;
	}

	@Override
	public void setStatusValue(EnumUserStatus status)
	{
		for (int i = 0; i < statusFilter.getItemCount(); i++)
		{
			String val = statusFilter.getValue(i);

			if (!val.isEmpty() && EnumUserStatus.valueOf(val) != null && EnumUserStatus.valueOf(val).equals(status))
			{
				statusFilter.setSelectedIndex(i);

				String token = History.getToken();
				token = TokenUtils.setToken(token, "filter", val);
				History.newItem(token, false);
				return;
			}
		}

	}
	
	@UiHandler("statusFilter")
	void onStatusChanged(ChangeEvent event)
	{
		String token = History.getToken();

		if (getStatusFilterValue() != null)
		{
			token = TokenUtils.setToken(token, "filter", getStatusFilterValue().toString());
		}
		else
		{
			token = TokenUtils.setToken(token, "filter", "");
			// token = TokenUtils.removeToken(token, "filter");
		}
		// reset paging...
		token = TokenUtils.removeToken(token, "page");
		History.newItem(token);

	}
	
	@Override
	public String translate(String key)
	{
		try
		{
			return Webetek.getConstants().getString(key);
		}
		catch (MissingResourceException e)
		{
			return key;
		}
	}

}
